<?php
/**
 * Created by PhpStorm.
 * User: yxs
 * Date: 2017/6/26
 * Time: 10:21
 */

namespace app\shop\controller;

use icontroller\wechat\AController;
use library\common\str;
use model\ShopCollect;
use model\ShopGoods;
use model\ShopUserCart;
use think\Controller;
use model\ShopStore as st;
use think\Db;

class Shop extends AController
{
    public function index()
    {
        return view();
    }

    // <editor-fold defaultstate="collapsed" desc="店铺列表">
    public function get_list()
    {

        $page = input('page', 0);                      //分页
        $count = config('paginate.list_rows');         //每页数量
        $city_searchValue = input("city_searchValue"); //城市查询
//      $sort = input('sort', 'desc');
//      $order = input('order', 'id');
        $keyword = input('keyword', '');
        $lat =  $this->account["current_lat"];
        $lng = $this->account["current_lng"];
        $where = [
            "flag_lock" => 0
        ];
        if (!empty($keyword)) {
            $where["name|description"] = ["like", "%$keyword%"];
        }
        if ($city_searchValue) {
            $where["city"] = $city_searchValue;
        }

        $mo = new st();
        $order = "id desc";
        $field = "*";
        if (!empty($lat) && !empty($lng)) {
            $order = "distance asc";
            $field = "*,ROUND((ACOS(SIN(($lat * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) + COS(($lat * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(($lng * 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6380),2) as distance";
        }

        $list = $mo->field($field)
            ->order($order)->where($where)->page($page, $count)->select();

        $suc = new ShopUserCart();
        foreach ($list as &$item){
            $count = $suc->alias("suc")->join("shop_goods sg","sg.id = suc.goods_id","LEFT")->where(["suc.account_id"=>$this->account_id,"suc.flag_delete"=>0, "sg.store_id"=>$item["id"]])->sum("quantity");
            if ($item["flag_lock"]==1||!str::time_quantum($item["bizhours_start"],$item["bizhours_end"])){
                $item["is_work"] = 0; //歇业中
            }else{
                $item["is_work"] = 1; //营业中
            }
            $item["count"] = empty($count)?"":$count;
            if (!isset($item["distance"])){
                $item["distance"]=0;
            }
        }
        $this->success("成功", null, $list);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="收藏店铺">
    public function collect_store()
    {
        $store_id = input("store_id");
        $store = ShopCollect::get(["store_id" => $store_id, "account_id" => $this->account_id]);
        if ($store) {
            $this->error("店铺已收藏");
        }
        $result = ShopCollect::create(["store_id" => $store_id, "account_id" => $this->account_id]);
        if (!$result) {
            $this->error("收藏出错");
        }
        $this->success("收藏成功");
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="取消收藏">
    public function del_collect_store()
    {
        $store_id = input("store_id");
        $store = ShopCollect::get(["store_id" => $store_id, "account_id" => $this->account_id]);
        if ($store) {
            $result = ShopCollect::destroy(["store_id" => $store_id, "account_id" => $this->account_id]);
            if (!$result) {
                $this->error("取消收藏出错");
            }
        }
        $this->success("取消成功");
    }
    // </editor-fold>
}